package com.example.block_chain.controller.admin;

import com.example.block_chain.common.AppHttpCodeEnum;
import com.example.block_chain.common.ResponseResult;
import com.example.block_chain.domain.dto.AwardAuditingDto;
import com.example.block_chain.domain.pojo.Admin;
import com.example.block_chain.service.admin.AwardAuditingService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpSession;
import java.io.UnsupportedEncodingException;

@RestController
public class AwardAuditingController {

    @Autowired
    private AwardAuditingService awardAuditingService;

    //审核奖状接口
    @GetMapping("/select/audit/award")
    public ResponseResult selectAuditAward(@RequestParam String classCode,
                                           @RequestParam String facultyCode,
                                           @RequestParam String schoolCode,
                                           @RequestParam Integer status,
                                           @RequestParam Integer page,
                                           @RequestParam Integer pageSize, HttpSession session) throws UnsupportedEncodingException {
        if (session.getAttribute("admin") == null) return ResponseResult.errorResult(AppHttpCodeEnum.NEED_LOGIN);
        return awardAuditingService.selectAuditAward(classCode, facultyCode,
                schoolCode, status, page, pageSize, session);
    }

    @PostMapping("/counsellor/select/audit/award/")
    public ResponseResult counsellorSelectAuditAward(@RequestBody AwardAuditingDto dto) throws UnsupportedEncodingException {
        String classCode = dto.getClassCode();
        String facultyCode = dto.getFacultyCode();
        String schoolCode = dto.getSchoolCode();

        String status = dto.getStatus();

        Integer pageSize = dto.getPageSize();
        Integer page = dto.getPage();

        return awardAuditingService.counsellorSelectAuditAward(classCode, facultyCode,
                schoolCode, status, page, pageSize);
    }

    @PostMapping("/faculty/select/audit/award/")
    public ResponseResult facultySelectAuditAward(@RequestBody AwardAuditingDto dto) throws UnsupportedEncodingException {
        String facultyCode = dto.getFacultyCode();
        String schoolCode = dto.getSchoolCode();
        String status = dto.getStatus();

        Integer pageSize = dto.getPageSize();
        Integer page = dto.getPage();
        return awardAuditingService.facultySelectAuditAward(facultyCode, schoolCode, status, page, pageSize);
    }

    @PostMapping("/school/select/audit/award/")
    public ResponseResult schoolSelectAuditAward(@RequestBody AwardAuditingDto dto) throws UnsupportedEncodingException {
        String schoolCode = dto.getSchoolCode();
        String status = dto.getStatus();

        Integer pageSize = dto.getPageSize();
        Integer page = dto.getPage();
        return awardAuditingService.schoolSelectAuditAward(schoolCode, status, page, pageSize);
    }

    @PostMapping("/pass/award/")
    public ResponseResult passAward(@RequestBody AwardAuditingDto dto, HttpSession session) throws UnsupportedEncodingException {
        Admin admin = (Admin) session.getAttribute("admin");
        if (admin == null) return ResponseResult.errorResult(AppHttpCodeEnum.NEED_LOGIN);
        String awardId = dto.getAwardId();
        Integer authority = Integer.parseInt(admin.getAuthority());
        String aNo = admin.getANo();
        return awardAuditingService.passAward(awardId, authority, aNo);
    }

    @PostMapping("/refuse/award/")
    public ResponseResult refuseAward(@RequestBody AwardAuditingDto dto, HttpSession session) {
        String awardId = dto.getAwardId();
        Admin admin = (Admin) session.getAttribute("admin");
        if (admin == null) return ResponseResult.errorResult(AppHttpCodeEnum.NEED_LOGIN);
        Integer authority = Integer.parseInt(admin.getAuthority());
        String aNo = admin.getANo();
        return awardAuditingService.refuseAward(awardId, authority, aNo);
    }
}
